package com.example.administrator.myapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {

    private CheckBox shuttlecock;
    private CheckBox basketball;
    private CheckBox pingpang;
    private TextView hobby;
    private String hobby1;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initData();
        initEvent();

    }

    private void initView() {
        shuttlecock = findViewById(R.id.like_shuttlecock);
        basketball = findViewById(R.id.like_basketball);
        pingpang = findViewById(R.id.like_pingpang);
        hobby = findViewById(R.id.hobby);
    }

    private void initData() {
        //用于存放被选中的文件
        hobby1 = new String();
    }

    private void initEvent() {
        shuttlecock.setOnCheckedChangeListener(this);
        basketball.setOnCheckedChangeListener(this);
        pingpang.setOnCheckedChangeListener(this);
    }

    @Override
    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
        String motion = compoundButton.getText().toString();//获取被点击的文本信息
        if (b) {
            if (!hobby1.contains(motion)) {
                hobby1 = hobby1 + motion;
                hobby.setText(hobby1);

            }
        } else {
            if (hobby1.contains(motion)) {
                hobby1 = hobby1.replace(motion, "");
                hobby.setText(hobby1);

            }

        }
    }
}
